﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using MyHelper;
using clf.weighing.entity;


namespace weighing
{
    public class CameraInfoDao : BaseDao<CameraInfo>
    {
        #region instance

        private static CameraInfoDao? instance;
        public static CameraInfoDao Instance => instance ??= new CameraInfoDao();

        public override string GetTableName()
        {
            return TableName.camera_info.ToString();
        }
        #endregion

        /// <summary>
        /// 只获取本端的
        /// </summary>
        /// <returns></returns>
        public List<CameraInfo> GetList()
        {
            String conditon = $"{CameraInfoField.client_id} = {App.InitConfig.clientId}";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, conditon);
            var data = Sqlite.Select<CameraInfo>(sql);
            return data;
        }

        public bool IsExist(CameraInfo camera, bool isEdit = false)
        {
            String condition = CameraInfoField.ip.ToString() + $" = '{camera.ip}' and {CameraInfoField.port} = {camera.port} and {CameraInfoField.client_id} = {App.InitConfig.clientId}";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id", condition, null, null, null, 1, 0);
            var data = Sqlite.Select<CameraInfo>(sql);
            if (data.Count > 0)
            {
                if (isEdit)
                {
                    return !data[0].id.Equals(camera.id);
                }
                else
                {
                    return data[0] != null;
                }
            }
            else
            {
                return false;
            }
        }

        public int Delete(CameraInfo? camera, Boolean isTrue = false)
        {
            if (camera == null)
            {
                return 0;
            }

            if (isTrue)
            {
                return Sqlite.Delete(camera, isTrue);
            }
            else
            {
                String condition = CameraInfoField.id.ToString() + $" = {camera.id}";
                String time = DateTimeHelper.getCurrentDateTime();
                String set = $" {CameraInfoField.is_delete} = 1 " + $", {CameraInfoField.delete_time} = '{time}'";
                String sql = SqlBuilder.GetUpdateSql(GetTableName(), set, condition);
                return Sqlite.Delete(sql);
            }
        }

        public int ShowOrHide(CameraInfo cameraInfo)
        {
            String condition = CameraInfoField.id.ToString() + $" = {cameraInfo.id}";
            var time = DateTimeHelper.GetTimeStamp();
            String set = $" {CameraInfoField.is_play} = {cameraInfo.isPlay} " + $", {CameraInfoField.sync_time} = '{time}'";
            String sql = SqlBuilder.GetUpdateSql(GetTableName(), set, condition);
            return Sqlite.Update(sql);
        }

        public List<CameraInfo> GetShowList()
        {
            String conditon = $"{CameraInfoField.client_id} = {App.InitConfig.clientId} and {CameraInfoField.is_play} =1 ";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, conditon);
            var data = Sqlite.Select<CameraInfo>(sql);
            return data;
        }

        public List<CameraInfo> SelectRecognizeCameras()
        {
            String conditon = $"{CameraInfoField.client_id} = {App.InitConfig.clientId} and {CameraInfoField.is_recognize} =1 ";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, conditon);
            var data = Sqlite.Select<CameraInfo>(sql);
            return data;
        }
    }
}
